package com.zzf.concurrent.example.sync.bean;

import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;

/**
 * @author GaoFeng2017
 * @date 2019/8/4 22:14
 */
@Slf4j
public class TicketRunnable implements Runnable {

    private static final int MAX = 500;
    private int currentIndex = 1;
    private LinkedList<Integer> ids = new LinkedList<>();

    /** monitor */
    private final Object LOCK = new Object();

    @Override
    public void run() {
        while(true) {
            synchronized (LOCK) {
                if (currentIndex > MAX) {
                    break;
                }
                // TimeUtil.sleep(1);
                int value = currentIndex++;
                log.info("{}开始叫号，当前号数为{}", Thread.currentThread().getName(), value);
                ids.add(value);
                try {
                    Thread.sleep((long) (Math.random() * 100L));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // TimeUtil.sleep(100000);
            }
        }

        log.info("ids size is {}", ids.size());
    }
}
